package com.lujianfei.filesend.ui.search

import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import com.lujianfei.common.DensityUtils
import com.lujianfei.common.KeyboardUtils
import com.lujianfei.filesend.MyApplication
import com.lujianfei.filesend.R
import com.lujianfei.filesend.base.BaseDialogFragment
import com.lujianfei.filesend.databinding.DialogClientConnectBinding
import com.lujianfei.filesend.ui.search.viewmodel.ClientConnectViewModel


/**
 * Author: lujianfei
 * Date: 2023/10/17 16:58
 * Description: 客户端连接对话框
 */

class ClientConnectDialog(val onConfirm:(String)->Unit, val onCancel:()->Unit): BaseDialogFragment<DialogClientConnectBinding>() {

    companion object {
        fun start(fragmentManager: FragmentManager, onConfirm:(String)->Unit, onCancel:()->Unit) {
            ClientConnectDialog(onConfirm = onConfirm, onCancel = onCancel).showNow(fragmentManager, "ClientConnectDialog")
        }
    }

    private val viewModel: ClientConnectViewModel by lazy { buildViewModel() }
    override fun resourceId() = R.layout.dialog_client_connect

    override fun initView() {
        binding?.vm = viewModel
    }

    override fun initData() {
        KeyboardUtils.showKeyboard(binding?.editMessage)
    }

    override fun initEvent() {
        binding?.onConfirmClick = View.OnClickListener {
            onConfirm.invoke(viewModel.ipNumber.get()?:"")
            dismiss()
        }
        binding?.onCancelClick = View.OnClickListener {
            onCancel.invoke()
            dismiss()
        }
    }

    override fun onStart() {
        val width = DensityUtils.getScreenWidth(MyApplication.instance) - 100
        val height = ViewGroup.LayoutParams.WRAP_CONTENT
        dialog?.window?.setLayout(width, height)
        dialog?.setCanceledOnTouchOutside(true)
        super.onStart()
    }
}